package com.imooc.reader.portal.config;

import com.imooc.reader.portal.dto.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseDTO> handleInternalException(Exception e) {
        log.error("",e);
        if (e instanceof NoResourceFoundException) {
            return new ResponseEntity<>(new ResponseDTO(e), HttpStatus.NOT_FOUND);
        } else {
            return new ResponseEntity<>(new ResponseDTO(e), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
